package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.po.EmployeePO;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Api(tags = "员工相关接口")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     * @param source
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO source) {
        log.info("员工登录：{}", source);

        EmployeePO employeePO = employeeService.login(source);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employeePO.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employeePO.getId())
                .userName(employeePO.getUsername())
                .name(employeePO.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation(value = "员工退出")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增员工
     * @param source
     * @return
     */
    @ApiOperation(value = "新增员工")
    @PostMapping("")
    public Result<Boolean> newEmployee(@RequestBody@Validated EmployeeDTO source){
        return Result.success(employeeService.newEmployee(source));
    }

    /**
     * 按条件分页查询员工
     * @param source
     * @return
     */
    @ApiOperation(value = "按条件分页查询员工")
    @GetMapping("/page")
    public Result<PageResult> pageQuery(EmployeePageQueryDTO source){
        return Result.success(employeeService.pageQuery(source));
    }

    /**
     * 启用/禁用用户
     * @param status
     * @param id
     * @return
     */
    @ApiOperation(value = "启用/禁用员工")
    @PostMapping("/status/{status}")
    public Result<Boolean> startOrStop(@PathVariable("status") Integer status,Long id){
        employeeService.startOrStop(status,id);
        return Result.success();
    }

    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询用户信息")
    @GetMapping("/{id}")
    public Result<EmployeeDTO> getById(@PathVariable("id")Long id){
        return Result.success(employeeService.getById(id));
    }

    /**
     * 更新员工信息
     * @param source
     * @return
     */
    @ApiOperation(value = "更新员工信息")
    @PutMapping("")
    public Result<Boolean> update(@RequestBody EmployeeDTO source){
        return Result.success(employeeService.update(source));
    }
}
